package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description NursingProjectServiceImpl
 * @Author zhangsuye
 * @Date 2024-10-15
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     * 查询护理项目列表
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 开启分页插件
        PageHelper.startPage(pageNum,pageSize);
        // 执行分页查询
        Page<NursingProjectVo> nursingProjectVoPage = nursingProjectMapper.findByCondition(name,status);
        // 封装PageResponse<> 返回值
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(nursingProjectVoPage);
        pageResponse.setRecords(nursingProjectVoPage.getResult());
        // 返回结果
        return pageResponse;
    }

    /**
     * 新增护理项目
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        // 创建护理项目实体类
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        // 字段填充 （mybatis拦截器自动填充公共字段）
        try {
            // 新增入库
            nursingProjectMapper.insert(nursingProject);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NAME_ALREADY_EXIST);
        }
    }

    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.findById(id);
    }

    /**
     * 更新护理项目
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        // 创建护理项目实体对象
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        try {
            // 更新数据库DuplicateKeyException
            nursingProjectMapper.update(nursingProject);
        } catch (DuplicateKeyException e) {
            throw new BaseException(BasicEnum.NAME_ALREADY_EXIST);
        }
    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 启用/禁用护理项目
     * @param id
     * @param status
     */
    @Override
    public void startOrStopStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatusById(id,status);
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {

        return nursingProjectMapper.findList();
    }
}
